package com.xps.and.yuntong.Ui;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.sdk.android.oss.ClientConfiguration;
import com.alibaba.sdk.android.oss.OSS;
import com.alibaba.sdk.android.oss.OSSClient;
import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;
import com.bumptech.glide.Glide;
import com.xps.and.yuntong.Constants;
import com.xps.and.yuntong.R;
import com.xps.and.yuntong.Utils.AssetsUitls;
import com.xps.and.yuntong.Utils.BitmapUtil;
import com.xps.and.yuntong.Utils.CommonUtils;
import com.xps.and.yuntong.Utils.DensityUtil;
import com.xps.and.yuntong.Utils.FileUtil;
import com.xps.and.yuntong.Utils.MyReceiver;
import com.xps.and.yuntong.View.Custom.WheelView;
import com.xps.and.yuntong.aly.OssService;
import com.xps.and.yuntong.aly.STSGetter;
import com.xps.and.yuntong.aly.UIDisplayer;
import com.xps.and.yuntong.base.BaseActivity;
import com.jude.utils.JUtils;
import com.zhy.base.fileprovider.FileProvider7;

import org.json.JSONException;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by administrator on 2017-12-09.
 */

public class PersonalCheliangZiliaoActivity extends BaseActivity implements View.OnClickListener,MyReceiver.Message {
    private static final String[] PLANETS = new String[]{"1", "2", "3", "4", "5", "6"};
    private static final String[] CLColoer = new String[]{"红色", "黄色", "香槟色", "银色", "黑色", "白色", "绿色", "棕色", "灰色", "紫色"};
    private static final String TAG = "PersonalCheliangZiliaoActivity";
    private static final int REQUEST_DRIVER_LICENSE = 1;//驾驶证
    private static final int REQUEST_DRIVER_ID_FRONT = 2;//正面
    private static final int REQUEST_DRIVER_ID_BACK = 3;//反面
    private static final int REQUEST_DRIVER_ID_BACKXS = 4;//行驶证
    private static final int REQUEST_DRIVER_ID_BAODAN = 5;//保单
    private static final int REQUEST_DRIVER_ID_INfo = 6;//身份信息
    private static int REQUEST_CODE = 0;

    private ImageView iv_person_ziliao_fanhui, iv_shenfen_zhengmian, iv_shenfen_fanmian, iv_jiashi, iv_xingshi, iv_cheliang_baodan, iv_cheliang_info;
    private LinearLayout ll_shangchuan_zhengmian, ll_shangchuan_fanmian, ll_shangchuan_jianshi, ll_shangchuan_xingshi, ll_shangchuan_cheliang_baodan, ll_shangchuan_cheliang_info;

    private TextView tv_choose_chepai, tv_choose_chexing, tv_choose_chese, tv_choose_cheling,tv_xiugai;
    private EditText et_choose_chepai, et_choose_chexing, et_choose_chese, et_choose_cheling;
    private ImageView iv_shangchuan_zhengmian,iv_shangchuan_fanmian,iv_shangchuan_jiashi,iv_shangchuan_xingshi,iv_shangchuan_cheliang_baodan,iv_shangchuan_cheliang_info;
    private TextView tv_shangchuan_zhengmian,tv_shangchuan_fanmian,tv_shangchuan_jiashi,tv_shangchuan_xingshi,tv_shangchuan_cheliang_baodan,tv_shangchuan_cheliang_info;

    MyReceiver myReceiver;
    private String panduanxuanze;
    private Dialog bottomDialog;
    View contentView;
    TextView XiCe_pickPhoto;
    TextView cancel_TextView;
    AlertDialog normalDia;

    String phoneNumber="15110200250";
    //图片标记
    int successPicCount = 0;//已经成功的个数
    int actualPicCount = 0;//应该成功的个数
    HashMap<String, ArrayList<String>> data;
    List<String> keyset = new ArrayList<>();

    private String picturePath;
    private String pictureName;
    private String xjxc = "1";

    private OssService ossService;
    private UIDisplayer displayer;
    @Override
    protected int getLayout() {
        return R.layout.activity_person_cheliangziliao;
    }

    @Override
    protected void setData() {
        displayer = new UIDisplayer(this);
        ossService = initOSS(Constants.endpoint, displayer);
//        ossService.setCallbackAddress(callbackAddress);
        initDiZhi();
        myReceiver = new MyReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("com.nangch.broadcasereceiver.MYRECEIVER");
        registerReceiver(myReceiver, intentFilter);
        myReceiver.setMessage(PersonalCheliangZiliaoActivity.this);
    }

    @Override
    protected void init(Bundle inBundle) {
        initView();
        intDialog();
        initDialog1();
        String headImg = JUtils.getSharedPreference().getString("headImg", "");
        Log.e("headImg", headImg);
    }
    @Override
    public void photoUploadSuccess() {
        successPicCount++;
        Log.e("111","已经成功的个数"+successPicCount+"应该成功的个数"+actualPicCount+"photoUploadSuccess");
        if (actualPicCount <= successPicCount) {
            dissmisDialog();
            normalDia.dismiss();
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        displayer = null;
        ossService = null;
    }


    private void initView() {

        iv_person_ziliao_fanhui = (ImageView) findViewById(R.id.iv_person_ziliao_fanhui);
        iv_shenfen_zhengmian = (ImageView) findViewById(R.id.iv_shenfen_zhengmian);
        iv_shenfen_fanmian = (ImageView) findViewById(R.id.iv_shenfen_fanmian);
        iv_jiashi = (ImageView) findViewById(R.id.iv_jiashi);
        iv_xingshi = (ImageView) findViewById(R.id.iv_xingshi);
        iv_cheliang_baodan = (ImageView) findViewById(R.id.iv_cheliang_baodan);
        iv_cheliang_info = (ImageView) findViewById(R.id.iv_cheliang_info);
        iv_shangchuan_zhengmian = (ImageView) findViewById(R.id.iv_shangchuan_zhengmian);
        iv_shangchuan_fanmian = (ImageView) findViewById(R.id.iv_shangchuan_fanmian);
        iv_shangchuan_jiashi = (ImageView) findViewById(R.id.iv_shangchuan_jiashi);
        iv_shangchuan_xingshi = (ImageView) findViewById(R.id.iv_shangchuan_xingshi);
        iv_shangchuan_cheliang_baodan = (ImageView) findViewById(R.id.iv_shangchuan_cheliang_baodan);
        iv_shangchuan_cheliang_info = (ImageView) findViewById(R.id.iv_shangchuan_cheliang_info);

        ll_shangchuan_zhengmian = (LinearLayout) findViewById(R.id.ll_shangchuan_zhengmian);
        ll_shangchuan_fanmian = (LinearLayout) findViewById(R.id.ll_shangchuan_fanmian);
        ll_shangchuan_jianshi = (LinearLayout) findViewById(R.id.ll_shangchuan_jianshi);
        ll_shangchuan_xingshi = (LinearLayout) findViewById(R.id.ll_shangchuan_xingshi);
        ll_shangchuan_cheliang_baodan = (LinearLayout) findViewById(R.id.ll_shangchuan_cheliang_baodan);
        ll_shangchuan_cheliang_info = (LinearLayout) findViewById(R.id.ll_shangchuan_cheliang_info);

        tv_choose_chepai = (TextView) findViewById(R.id.tv_choose_chepai);
        tv_choose_chexing = (TextView) findViewById(R.id.tv_choose_chexing);
        tv_choose_chese = (TextView) findViewById(R.id.tv_choose_chese);
        tv_choose_cheling = (TextView) findViewById(R.id.tv_choose_cheling);
        tv_shangchuan_zhengmian = (TextView) findViewById(R.id.tv_shangchuan_zhengmian);
        tv_shangchuan_fanmian = (TextView) findViewById(R.id.tv_shangchuan_fanmian);
        tv_shangchuan_jiashi = (TextView) findViewById(R.id.tv_shangchuan_jiashi);
        tv_shangchuan_xingshi = (TextView) findViewById(R.id.tv_shangchuan_xingshi);
        tv_shangchuan_cheliang_baodan = (TextView) findViewById(R.id.tv_shangchuan_cheliang_baodan);
        tv_shangchuan_cheliang_info = (TextView) findViewById(R.id.tv_shangchuan_cheliang_info);
        tv_xiugai= (TextView) findViewById(R.id.tv_xiugai);
        
        et_choose_chepai = (EditText) findViewById(R.id.et_choose_chepai);
        et_choose_chexing = (EditText) findViewById(R.id.et_choose_chexing);
        et_choose_chese = (EditText) findViewById(R.id.et_choose_chese);
        et_choose_cheling = (EditText) findViewById(R.id.et_choose_cheling);


        iv_person_ziliao_fanhui.setOnClickListener(this);
        ll_shangchuan_zhengmian.setOnClickListener(this);
        ll_shangchuan_fanmian.setOnClickListener(this);
        ll_shangchuan_jianshi.setOnClickListener(this);
        ll_shangchuan_xingshi.setOnClickListener(this);
        ll_shangchuan_cheliang_baodan.setOnClickListener(this);
        ll_shangchuan_cheliang_info.setOnClickListener(this);
        tv_choose_chepai.setOnClickListener(this);
        tv_choose_chexing.setOnClickListener(this);
        tv_choose_chese.setOnClickListener(this);
        tv_choose_cheling.setOnClickListener(this);




        tv_xiugai.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){

            case R.id.iv_person_ziliao_fanhui:

                finish();
                break;

            case R.id.ll_shangchuan_zhengmian:
                REQUEST_CODE = REQUEST_DRIVER_ID_FRONT;
                bottomDialog.show();

                iv_shangchuan_zhengmian.setVisibility(View.GONE);
                tv_shangchuan_zhengmian.setVisibility(View.GONE);

                iv_shenfen_zhengmian.setImageResource(R.drawable.just_img);
                iv_shenfen_zhengmian.setVisibility(View.VISIBLE);
                XiCe_pickPhoto.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bottomDialog.dismiss();
                        selectDialog();
                    }
                });
                cancel_TextView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bottomDialog.dismiss();
                    }
                });

                break;
            case R.id.ll_shangchuan_fanmian:

                REQUEST_CODE = REQUEST_DRIVER_ID_BACK;
                bottomDialog.show();

                iv_shangchuan_fanmian.setVisibility(View.GONE);
                tv_shangchuan_fanmian.setVisibility(View.GONE);

                iv_shenfen_fanmian.setImageResource(R.drawable.back_img);
                iv_shenfen_fanmian.setVisibility(View.VISIBLE);
                XiCe_pickPhoto.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bottomDialog.dismiss();
                        selectDialog();
                    }
                });
                cancel_TextView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bottomDialog.dismiss();
                    }
                });

                break;
            case R.id.ll_shangchuan_jianshi:

                REQUEST_CODE = REQUEST_DRIVER_LICENSE;
                bottomDialog.show();
                iv_shangchuan_jiashi.setVisibility(View.GONE);
                tv_shangchuan_jiashi.setVisibility(View.GONE);

                iv_jiashi.setImageResource(R.drawable.driving_img);
                iv_jiashi.setVisibility(View.VISIBLE);
                XiCe_pickPhoto.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bottomDialog.dismiss();
                        selectDialog();
                    }
                });
                cancel_TextView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bottomDialog.dismiss();
                    }
                });

                break;
            case R.id.ll_shangchuan_xingshi:
                REQUEST_CODE = REQUEST_DRIVER_ID_BACKXS;
                bottomDialog.show();

                iv_xingshi.setImageResource(R.drawable.driving_img);
                iv_shangchuan_xingshi.setVisibility(View.GONE);
                tv_shangchuan_xingshi.setVisibility(View.GONE);

                iv_xingshi.setVisibility(View.VISIBLE);
                XiCe_pickPhoto.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bottomDialog.dismiss();
                        selectDialog();
                    }
                });
                cancel_TextView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bottomDialog.dismiss();
                    }
                });


                break;
            case R.id.ll_shangchuan_cheliang_baodan:

                REQUEST_CODE = REQUEST_DRIVER_ID_BAODAN;
                bottomDialog.show();

                iv_shangchuan_cheliang_baodan.setVisibility(View.GONE);
                tv_shangchuan_cheliang_baodan.setVisibility(View.GONE);

                iv_cheliang_baodan.setImageResource(R.drawable.bd_img);
                iv_cheliang_baodan.setVisibility(View.VISIBLE);
                XiCe_pickPhoto.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bottomDialog.dismiss();
                        selectDialog();
                    }
                });
                cancel_TextView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bottomDialog.dismiss();
                    }
                });



                break;
            case R.id.ll_shangchuan_cheliang_info:

                REQUEST_CODE = REQUEST_DRIVER_ID_INfo;
                bottomDialog.show();

                iv_shangchuan_cheliang_info.setVisibility(View.GONE);
                tv_shangchuan_cheliang_info.setVisibility(View.GONE);

                iv_cheliang_info.setImageResource(R.drawable.cheliang_img);
                iv_cheliang_info.setVisibility(View.VISIBLE);
                XiCe_pickPhoto.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bottomDialog.dismiss();
                        selectDialog();
                    }
                });
                cancel_TextView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bottomDialog.dismiss();
                    }
                });




                break;
            case R.id.tv_choose_chepai:

                initCarPlateChooseDialog();
                break;
            case R.id.tv_choose_chexing:
                Intent i = new Intent(PersonalCheliangZiliaoActivity.this, CarBrandList.class);
                startActivity(i);

                break;
            case R.id.tv_choose_chese:
                panduanxuanze = "CLColoer";
                showCustomizeDialog(CLColoer);

                break;
            case R.id.tv_choose_cheling:
                panduanxuanze = "PLANETS";
                showCustomizeDialog(PLANETS);

                break;


            case R.id.tv_xiugai:

                //if修改成功
//                Intent intent=new Intent(PersonalCheliangZiliaoActivity.this,PersonalZiliaoActivity.class);
//                startActivity(intent);
//                finish();

                break;

        }
    }



    void initCarPlateChooseDialog() {
        View outerView = LayoutInflater.from(PersonalCheliangZiliaoActivity.this).inflate(R.layout.car_plate_choose_dialog, null);
        final WheelView wv_provence_brief = (WheelView) outerView.findViewById(R.id.wv_provence_brief);
        final WheelView wv_word = (WheelView) outerView.findViewById(R.id.wv_model);
        wv_provence_brief.setOffset(3);
        try {
            wv_provence_brief.setItems(AssetsUitls.getInstance(PersonalCheliangZiliaoActivity.this).getProvences());
        } catch (IOException e) {
            e.printStackTrace();
        }
        wv_provence_brief.setSeletion(1);
        wv_provence_brief.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                //tv_prov_brief.setText(item);
            }
        });

        wv_word.setOffset(3);
        wv_word.setItems(Arrays.asList(CommonUtils.WORDS));
        wv_word.setSeletion(1);
        new AlertDialog.Builder(PersonalCheliangZiliaoActivity.this)
                .setTitle("请选择：")
                .setView(outerView)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        et_choose_chepai.setText(wv_provence_brief.getSeletedItem() + wv_word.getSeletedItem());
                        tv_choose_chepai.setText("    " + wv_provence_brief.getSeletedItem() + " " + wv_word.getSeletedItem() + ":    ");
                    }
                })
                .show();

    }


    @Override
    public void getMsg(String getCouponId) {

        et_choose_chexing.setText(getCouponId);
    }

    public void showCustomizeDialog(String[] PLANETS) {
        View outerView = LayoutInflater.from(PersonalCheliangZiliaoActivity.this).inflate(R.layout.wheel_view_xin, null);
        WheelView wv = (WheelView) outerView.findViewById(R.id.picker_rel);
        wv.setOffset(2);
        wv.setItems(Arrays.asList(PLANETS));
        wv.setSeletion(0);
        wv.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                if (panduanxuanze.equals("CLColoer")) {
                    et_choose_chese.setText(item);
                } else if (panduanxuanze.equals("PLANETS")) {
                    et_choose_cheling.setText(item);
                }
            }
        });
        wv.onSeletedCallBack();
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(PersonalCheliangZiliaoActivity.this);
        if (panduanxuanze.equals("CLColoer")) {
            alertDialog.setTitle("选择车辆颜色");
        } else if (panduanxuanze.equals("PLANETS")) {
            alertDialog.setTitle("选择车辆车龄");
        }
        alertDialog.setView(outerView);
        alertDialog.setPositiveButton("确认", null);
        alertDialog.show();
    }


    private void intDialog() {

        bottomDialog = new Dialog(this, R.style.BottomDialog);
        contentView = LayoutInflater.from(this).inflate(R.layout.dialog, null);
        bottomDialog.setContentView(contentView);
        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) contentView.getLayoutParams();
        params.width = getResources().getDisplayMetrics().widthPixels - DensityUtil.dp2px(this, 16f);
        params.bottomMargin = DensityUtil.dp2px(this, 8f);
        contentView.setLayoutParams(params);
        bottomDialog.setCanceledOnTouchOutside(true);
        bottomDialog.getWindow().setGravity(Gravity.CENTER);
        bottomDialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation);
        XiCe_pickPhoto = (TextView) contentView.findViewById(R.id.ok);
        cancel_TextView = (TextView) contentView.findViewById(R.id.quxiao);
    }

    private void initDialog1(){
        final AlertDialog.Builder normalDialog123 =
                new AlertDialog.Builder(PersonalCheliangZiliaoActivity.this);
        normalDialog123.setTitle("温馨提示");
        normalDialog123.setMessage("图片正在上传请稍后。。。");
        normalDialog123.setCancelable(false);
        normalDia = normalDialog123.create();

    }

    void selectDialog() {
        final Dialog bottomDialog = new Dialog(this, R.style.BottomDialog);
        View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_photo, null);
        bottomDialog.setContentView(contentView);
        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) contentView.getLayoutParams();
        params.width = getResources().getDisplayMetrics().widthPixels - DensityUtil.dp2px(this, 16f);
        params.bottomMargin = DensityUtil.dp2px(this, 8f);
        contentView.setLayoutParams(params);
        bottomDialog.setCanceledOnTouchOutside(true);
        bottomDialog.getWindow().setGravity(Gravity.BOTTOM);
        bottomDialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation);
        bottomDialog.show();
        TextView XiCe_pickPhoto = (TextView) contentView.findViewById(R.id.XiCe_pickPhoto);
        XiCe_pickPhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                xjxc = "1";
                Intent i;
                switch (REQUEST_CODE) {
                    case REQUEST_DRIVER_LICENSE:
                        i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        startActivityForResult(i, REQUEST_DRIVER_LICENSE);
                        break;
                    case REQUEST_DRIVER_ID_FRONT:
                        i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        startActivityForResult(i, REQUEST_DRIVER_ID_FRONT);
                        break;
                    case REQUEST_DRIVER_ID_BACK:
                        i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        startActivityForResult(i, REQUEST_DRIVER_ID_BACK);
                        break;
                    case REQUEST_DRIVER_ID_BACKXS:
                        i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        startActivityForResult(i, REQUEST_DRIVER_ID_BACKXS);
                        break;
                    case REQUEST_DRIVER_ID_BAODAN:
                        i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        startActivityForResult(i, REQUEST_DRIVER_ID_BAODAN);
                        break;
                    case REQUEST_DRIVER_ID_INfo:
                        i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        startActivityForResult(i, REQUEST_DRIVER_ID_INfo);
                        break;
                }
                bottomDialog.dismiss();
            }
        });
        TextView XiJi_takePhoto = (TextView) contentView.findViewById(R.id.XiJi_takePhoto);
        XiJi_takePhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                xjxc = "2";
                Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                switch (REQUEST_CODE) {
                    case REQUEST_DRIVER_LICENSE:
                        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                            picturePath = "REQUEST_DRIVER_LICENSE.png";
                            pictureName = "REQUEST_DRIVER_LICENSE";

                            File file = new File(Environment.getExternalStorageDirectory(), picturePath);
                            picturePath = file.getAbsolutePath();
                            // 仅需改变这一行
                            Uri fileUri = FileProvider7.getUriForFile(PersonalCheliangZiliaoActivity.this, file);
                            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
                            startActivityForResult(takePictureIntent, REQUEST_DRIVER_LICENSE);
                        }
                        break;
                    case REQUEST_DRIVER_ID_FRONT:
                        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                            picturePath = "REQUEST_DRIVER_ID_FRONT.png";
                            pictureName = "REQUEST_DRIVER_ID_FRONT";

                            File file = new File(Environment.getExternalStorageDirectory(), picturePath);
                            picturePath = file.getAbsolutePath();
                            // 仅需改变这一行
                            Uri fileUri = FileProvider7.getUriForFile(PersonalCheliangZiliaoActivity.this, file);
                            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
                            startActivityForResult(takePictureIntent, REQUEST_DRIVER_ID_FRONT);
                        }
                        break;
                    case REQUEST_DRIVER_ID_BACK:
                        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                            picturePath = "REQUEST_DRIVER_ID_BACK.png";
                            pictureName = "REQUEST_DRIVER_ID_BACK";

                            File file = new File(Environment.getExternalStorageDirectory(), picturePath);
                            picturePath = file.getAbsolutePath();
                            // 仅需改变这一行
                            Uri fileUri = FileProvider7.getUriForFile(PersonalCheliangZiliaoActivity.this, file);
                            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
                            startActivityForResult(takePictureIntent, REQUEST_DRIVER_ID_BACK);
                        }
                        break;
                    case REQUEST_DRIVER_ID_BACKXS:
                        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                            picturePath = "REQUEST_DRIVER_ID_BACKXS.png";
                            pictureName = "REQUEST_DRIVER_ID_BACKXS";

                            File file = new File(Environment.getExternalStorageDirectory(), picturePath);
                            picturePath = file.getAbsolutePath();
                            // 仅需改变这一行
                            Uri fileUri = FileProvider7.getUriForFile(PersonalCheliangZiliaoActivity.this, file);
                            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
                            startActivityForResult(takePictureIntent, REQUEST_DRIVER_ID_BACKXS);
                        }
                        break;
                    case REQUEST_DRIVER_ID_BAODAN:
                        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                            picturePath = "REQUEST_DRIVER_ID_BAODAN.png";
                            pictureName = "REQUEST_DRIVER_ID_BAODAN";

                            File file = new File(Environment.getExternalStorageDirectory(), picturePath);
                            picturePath = file.getAbsolutePath();
                            // 仅需改变这一行
                            Uri fileUri = FileProvider7.getUriForFile(PersonalCheliangZiliaoActivity.this, file);
                            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
                            startActivityForResult(takePictureIntent, REQUEST_DRIVER_ID_BAODAN);
                        }
                        break;
                    case REQUEST_DRIVER_ID_INfo:
                        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                            picturePath = "REQUEST_DRIVER_ID_INfo.png";
                            pictureName = "REQUEST_DRIVER_ID_INfo";

                            File file = new File(Environment.getExternalStorageDirectory(), picturePath);
                            picturePath = file.getAbsolutePath();
                            // 仅需改变这一行
                            Uri fileUri = FileProvider7.getUriForFile(PersonalCheliangZiliaoActivity.this, file);
                            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
                            startActivityForResult(takePictureIntent, REQUEST_DRIVER_ID_INfo);
                        }
                        break;

                }
                bottomDialog.dismiss();
            }
        });

        TextView cancel_TextView = (TextView) contentView.findViewById(R.id.cancel_TextView);
        cancel_TextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bottomDialog.dismiss();
            }
        });
    }


    void doUploadOSS(String address) {
        actualPicCount++;
        Log.e("111","已经成功的个数"+successPicCount+"应该成功的个数"+actualPicCount+"doUploadOSS");
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date curDate = new Date(System.currentTimeMillis());
        String IDBackName = address + formatter.format(curDate) + "/" + phoneNumber + ".png";
        Log.e("111","IDBackName"+IDBackName+"phoneNumber"+phoneNumber+"picturePath"+picturePath);
        ossService.asyncPutImage(IDBackName, picturePath, phoneNumber);
    }

    void doUploadOSS_v2(String address) {
        actualPicCount++;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date curDate = new Date(System.currentTimeMillis());
        String IDBackName = address + formatter.format(curDate) + "/" + phoneNumber + ".png";
        ossService.asyncPutImage(IDBackName, getProcessedPicFile(pictureName), phoneNumber);
    }


    void processBitmap(ImageView iv) {


        int rotateDegree = FileUtil.getBitmapDegree(picturePath);
        if (rotateDegree != 0) {

            Observable.create((ObservableOnSubscribe<String>) e -> e.onNext(picturePath))
                    .map(s -> {
                        Bitmap bitcompress = BitmapUtil.getSmallBitmap(s, 480, 800);
                        Bitmap rotatedBitmap = BitmapUtil.rotateBitmapByDegree(bitcompress, 360 - (-rotateDegree));
                        CommonUtils.saveBitmapToSD(getProcessedPicFile(pictureName), rotatedBitmap);

                        final BitmapFactory.Options options = new BitmapFactory.Options();
                        options.inJustDecodeBounds = true;
                        options.inSampleSize = BitmapUtil.calculateInSampleSize(options, 240, 480);
                        options.inJustDecodeBounds = false;
                        return BitmapFactory.decodeFile(getProcessedPicFile(pictureName), options);

                    }).subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .doOnNext(new Consumer<Bitmap>() {
                        @Override
                        public void accept(@NonNull Bitmap bitmap) throws Exception {
                            switchLiscenseType();
                        }
                    })
                    .subscribe(new Consumer<Bitmap>() {
                        @Override
                        public void accept(@NonNull Bitmap bitmap) throws Exception {
                            iv.setImageBitmap(bitmap);
                        }
                    });
        } else {
            Observable.create((ObservableOnSubscribe<String>) e -> e.onNext(picturePath))
                    .map(s -> {
                        Bitmap bitcompress = BitmapUtil.getSmallBitmap(s, 480, 800);
                        CommonUtils.saveBitmapToSD(getProcessedPicFile(pictureName), bitcompress);

                        final BitmapFactory.Options options = new BitmapFactory.Options();
                        options.inJustDecodeBounds = true;
                        options.inSampleSize = BitmapUtil.calculateInSampleSize(options, 240, 480);
                        options.inJustDecodeBounds = false;

                        return BitmapFactory.decodeFile(getProcessedPicFile(pictureName), options);

                    }).subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .doOnNext(new Consumer<Bitmap>() {
                        @Override
                        public void accept(@NonNull Bitmap bitmap) throws Exception {
                            switchLiscenseType();
                        }
                    })
                    .subscribe(new Consumer<Bitmap>() {
                        @Override
                        public void accept(@NonNull Bitmap bitmap) throws Exception {
                            iv.setImageBitmap(bitmap);
                        }
                    });
        }
    }

    String getProcessedPicFile(String fileName) {
        String name = fileName + "_PROCESSED.png";
        return new File(Environment.getExternalStorageDirectory(), name).getAbsolutePath();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (xjxc.equals("1")) {
            if (data == null || resultCode != RESULT_OK) return;
            picturePath = CommonUtils.getRealFilePath(this, data.getData());
            switch (requestCode) {
                case REQUEST_DRIVER_LICENSE:
                    Glide.with(this).load(picturePath).into(iv_jiashi);
                    Log.e("111","已经成功的个数"+successPicCount+"应该成功的个数"+actualPicCount);
                    doUploadOSS("driverLicense/");
                    break;
                case REQUEST_DRIVER_ID_BACKXS:
                    Glide.with(this).load(picturePath).into(iv_xingshi);
                    doUploadOSS("drivingLicense/");
                    break;
                case REQUEST_DRIVER_ID_FRONT:
                    Glide.with(this).load(picturePath).into(iv_shenfen_zhengmian);
                    doUploadOSS("cardUp/");
                    break;
                case REQUEST_DRIVER_ID_BACK:
                    Glide.with(this).load(picturePath).into(iv_shenfen_fanmian);
                    doUploadOSS("cardDown/");
                    break;
                case REQUEST_DRIVER_ID_INfo:
                    Glide.with(this).load(picturePath).into(iv_shangchuan_cheliang_info);
                    doUploadOSS("drivingVehicle/");
                    break;
                case REQUEST_DRIVER_ID_BAODAN:
                    Glide.with(this).load(picturePath).into(iv_cheliang_baodan);
                    doUploadOSS("drivingGuarantee/");
                    break;
            }
        } else if (xjxc.equals("2")) {
            switch (requestCode) {
                case REQUEST_DRIVER_LICENSE:
                    processBitmap(iv_jiashi);
                    break;
                case REQUEST_DRIVER_ID_BACKXS:
                    processBitmap(iv_xingshi);
                    break;
                case REQUEST_DRIVER_ID_FRONT:
                    processBitmap(iv_shenfen_zhengmian);
                    break;
                case REQUEST_DRIVER_ID_BACK:
                    processBitmap(iv_shenfen_fanmian);
                    break;
                case REQUEST_DRIVER_ID_INfo:
                    processBitmap(iv_cheliang_info);
                    break;
                case REQUEST_DRIVER_ID_BAODAN:
                    processBitmap(iv_cheliang_baodan);
                    break;
            }
        }
    }




    public OssService initOSS(String endpoint, UIDisplayer displayer) {
        OSSCredentialProvider credentialProvider;
        //使用自己的获取STSToken的类

        if (Constants.stsServer.equals("")) {
            credentialProvider = new STSGetter();
        } else {
            credentialProvider = new STSGetter(Constants.stsServer);
        }

        ClientConfiguration conf = new ClientConfiguration();
        conf.setConnectionTimeout(15 * 1000); // 连接超时，默认15秒
        conf.setSocketTimeout(15 * 1000); // socket超时，默认15秒
        conf.setMaxConcurrentRequest(5); // 最大并发请求书，默认5个
        conf.setMaxErrorRetry(2); // 失败后最大重试次数，默认2次
        OSS oss = new OSSClient(this, endpoint, credentialProvider, conf);
        Log.e("111","socket连接上了么");
        return new OssService(oss, Constants.bucket, displayer);
    }



    void initDiZhi() {
        try {
            data = AssetsUitls.getInstance(PersonalCheliangZiliaoActivity.this).getCarBrandMap();
            Iterator<String> i = data.keySet().iterator();
            while (i.hasNext()) {
                keyset.add(i.next());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
    void switchLiscenseType() {
        switch (pictureName) {
            case "REQUEST_DRIVER_LICENSE":
                doUploadOSS_v2("driverLicense/");
                break;
            case "REQUEST_DRIVER_ID_BACKXS":
                doUploadOSS_v2("drivingLicense/");
                break;
            case "REQUEST_DRIVER_ID_FRONT":
                doUploadOSS_v2("cardUp/");
                break;
            case "REQUEST_DRIVER_ID_BACK":
                doUploadOSS_v2("cardDown/");
                break;
            case "REQUEST_DRIVER_ID_INfo":
                doUploadOSS_v2("drivingVehicle/");
                break;
            case "REQUEST_DRIVER_ID_BAODAN":
                doUploadOSS_v2("drivingGuarantee/");
                break;
        }
    }
}
